<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/15
 * Time: 15:52
 */

namespace app\mobile\controller;


use app\Model\FlagshipModel;
use app\Model\OrderGoodsModel;
use app\Model\OrderModel;
use app\Model\RebateLogModel;
use app\Model\UserLeverModel;
use app\Model\UsersModel;
use think\Controller;
//非继承的公共方法
class Publics extends Controller
{
    /*
     * 增加/减少用户累积消费金额（退款时减）
     * $money 金额
     * $jj 0加 1减
     */
    public function setAccuMoney($money=0,$jj=0)
    {
        $where=['user_id'=>session('user.user_id')];
        if ($jj==0){//加
//            dump(UsersModel::where($where)->find()->toArray());
            UsersModel::where($where)->setInc('accu_money',$money);
        }else{ //减
            UsersModel::where($where)->setDec('accu_money',$money);
        }
        //更新会员数据（可能等级提升或下降）
        $this->upLevel();
    }
    /*
     * 用户信息缓存（session）更新
     * $user_id 用户id
     */
    public function upSess($user_id=0)
    {
        if ($user_id==0){
            $getUser = UsersModel::find(session('user.user_id'));
        }else{
            $getUser = UsersModel::find($user_id);
        }
        $getUser['leve']=UserLeverModel::where('level_id',$getUser->level)->find()->toArray();
        session('user',$getUser);
    }
    /*
     * 检查消费金额更新会员等级
     *$user_id 用户id
     */
    public function upLevel($user_id=0)
    {
        if ($user_id==0){
            $getUser = UsersModel::find(session('user.user_id'));
        }else{
            $getUser = UsersModel::find($user_id);
        }

        //更新会员数据（可能等级提升或下降）
        $getLevel=UserLeverModel::where('amount','<=',$getUser->accu_money)->order('amount','desc')->find();
        if ($getLevel->level_id!=$getUser['level']){//会员等级需要更改
            $updata=['level'=>$getLevel->level_id,'user_id'=>$getUser['user_id']];
            UsersModel::update($updata);
        }

        $this->upSess($getUser['user_id']);//（session）更新
    }
    
    /*
     * 订单分销商佣金统计
     * $cartList  购物车信息
     */
    public function commission($cartList)
    {

       $data['commission_s1']=0;//订单上一级佣金
       $data['commission_s2']=0;//订单上二级佣金
        foreach ($cartList as $k=>$v){
            $data['commission_s1']+=$v['commission1']*$v['goods_num'];//商品佣金*数量
            $data['commission_s2']+=$v['commission2']*$v['goods_num'];//商品佣金*数量
        }
        return $data;
    }

    /*
   * 分销商佣金发放
   * $order_id  订单id
   */
    public function commission_send($order_id)
    {
       $getO= OrderModel::find($order_id);
        $where=['is_lock'=>0,
            'is_del'=>0];
        //一级
        if (session('user.first_leader')!=0&&$getO->commission1!=0){
            $user=UsersModel::where('user_id',session('user.first_leader'))->where($where)->find();
            if ($user){ //判断是否被冻结或删除
                UsersModel::where('user_id',session('user.first_leader'))->setInc('commission',$getO->commission1);
                //记录添加
                $this->commission_send_log(session('user.first_leader'),$getO,1);
            }
        }

        //二级
        if (session('user.first_leader')!=0&&$getO->commission2!=0){
            $user=UsersModel::where('user_id',session('user.second_leader'))->where($where)->find();
            if ($user){ //判断是否被冻结或删除
                UsersModel::where('user_id',session('user.second_leader'))->setInc('commission',$getO->commission2);
                $this->commission_send_log(session('user.second_leader'),$getO,2);
            }
        }
    }
    /*
     *
     * 佣金发放记录添加
     * $user_id 获得佣金的用户id
     * $getO  订单详情
     * $level 1-一级 2-二级
     */
    public function commission_send_log($user_id,$getO,$level)
    {
        $insert=[
            'user_id'=>$user_id,
            'buy_user_id'=>session('user.user_id'),
            'nickname'=>session('user.nickname'),
            'order_sn'=>$getO->order_sn,
            'order_id'=>$getO->order_id,
            'goods_price'=>$getO->goods_price,//订单所付金额
            'money'=>$getO->commission1,
            'level'=>$level,
            'create_time'=>time(),
            'confirm'=>time(),
        ];
        if ($level==2){
            $insert['money']=$getO->commission2;
        }

        RebateLogModel::insert($insert);
    }
}